package com.chanzany.common.exception;

import com.chanzany.common.result.R;
import com.chanzany.common.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @Author ChanZany
 * @Date 2021/4/11 17:28
 * @Version 1.0
 *
 * 原理：DispatchServlet.processHandlerException(...e...)->遍历容器中所有的HandlerExceptionResolver看看能不呢解析当前错误
 * -->HandlerExceptionResolver包含自动配置的ExceptionHandlerExceptionResolver,
 * 这个类会从所有自动获取所有ControllerAdvice类及其子类中带@ExceptionHandler的方法
 * doResolveHandlerMethodException(初始化:initExceptionHandlerAdviceCache()->ControllerAdviceBean.findAnnotatedBeans()拿到所有ControllerAdvice的Bean)->对每个ControllerAdvice Bean执行getExceptionHandlerMethod(handlerMethod, exception)
 *
 */
@RestControllerAdvice
@Slf4j
public class UnifiedExceptionHandler {


    @ExceptionHandler(value = Exception.class)
    public R handleException(Exception e) {
      log.error(e.getMessage());
      return R.error();
    }


    /**
     * 特定异常
     */
    @ExceptionHandler(BadSqlGrammarException.class)
    public R handleBadSqlGrammarException(BadSqlGrammarException e){
        log.error(e.getMessage(), e);
        return R.setResult(ResponseEnum.BAD_SQL_GRAMMAR_ERROR);
    }

    @ExceptionHandler(BusinessException.class)
    public R handlerBusinessException(BusinessException e){
        log.error(e.getMessage(),e);
        return R.error().message(e.getMessage()).code(e.getCode());
    }

    /**
     * 前端相关异常
     */
    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            HttpMediaTypeNotAcceptableException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            MethodArgumentNotValidException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public R handlerServletException(Exception e){
        log.error(e.getMessage(),e);
        return R.error().message(ResponseEnum.SERVLET_ERROR.getMessage())
                .code(ResponseEnum.SERVLET_ERROR.getCode());
    }
}
